# coding:utf-8

import requests
from bs4 import BeautifulSoup

# 2.1.1 Beautiful  Soup库的基本元素
r = requests.get('https://python123.io/ws/demo.html') # Demo网址
demo = r.text  # 抓取的数据
# print(demo)
# ??? 为什么第二个参数是html.parser不是lxml
soup = BeautifulSoup(demo, 'html.parser')
print(soup.prettify())
print(soup.a)
print(soup.title)
print(soup.a.name)
print(soup.a.parent.name)
print(soup.p.parent.name)
tag = soup.a
print(tag.attrs)
print(tag.attrs.get('class'))  # print(tag.attrs['class'])
print(tag.string, type(tag.string))
# utf-8 编码
new_soup = BeautifulSoup('<a>中文</a>', 'html.parser')
print(new_soup.a.string)

# 2.1.2 基于bs4库的HTML内容遍历方法
print(len(soup.contents), soup.contents)
print()
print(soup.body.content)  # None, body元素没有这个属性
print(len(soup.body.contents), soup.body.contents)
print(soup.head)
print('children=', soup.body.children)
for child in soup.body.children:
    print('child=', repr(child))
for child in soup.body.descendants:
    print('child2=', repr(child))

print(soup.title.parent)
print(soup.parent)  # None
for parent in soup.a.parents:
    if parent is None:
        print('parent=', parent)
    else:
        print('parent name=', parent.name)

print(soup.a.next_sibling)
print(soup.a.next_sibling.next_sibling)
print(soup.a.previous_sibling)
print(soup.a.previous_sibling.previous_sibling)

for sibling in soup.a.next_siblings:
    print('n sibling=', repr(sibling))

for sibling in soup.a.previous_siblings:
    print('p sibling=', repr(sibling))


# 2.1.3 基于bs4库的HTML内容的查找方法
r = requests.get('http://python123.io/ws/demo.html')
demo = r.text
soup = BeautifulSoup(demo, 'html.parser')
print(soup)
print()
print(soup.find_all('a'))
print(soup.find_all(['a', 'p']))
print()
print(soup.find_all('p', 'course'))
print(soup.find_all(id='link'))  # []
print(soup.find_all('p', recursive=False))  # []

# 2.1.4 实战：中国大学排名定向爬取
